package tongwentongshu.com.app.presenter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.util.ArrayMap;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;

import java.util.Map;

import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.BookDetailsBean;
import tongwentongshu.com.app.contract.CallBackColonyContract;
import tongwentongshu.com.app.contract.ReadingDetailsContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.AlertUtils;
import tongwentongshu.com.app.map.LocationOnSuccess;
import tongwentongshu.com.app.map.MapLocation;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/3.
 */


public class ReadingDetailsPresenter implements ReadingDetailsContract.Presenter {
    private ReadingDetailsContract.View readingDetailsView;
    private Map<String, String> map;
    private AMapLocation location;
    private MapLocation mapLocation;//定位对象

    public ReadingDetailsPresenter(ReadingDetailsContract.View readingDetailsView, Activity act) {
        this.readingDetailsView = readingDetailsView;
        mapLocation = new MapLocation(act);
        mapLocation.toLocation(locationOnSuccess);
    }

    public AMapLocation getAMapLocation() {
        return location;
    }

    //定位完毕回调
    public LocationOnSuccess locationOnSuccess = new LocationOnSuccess() {
        @Override
        public void onLocation(AMapLocation location) {
            if (null != location) {
                ReadingDetailsPresenter.this.location = location;
            }

        }
    };

    @Override
    public void initReadingDetails(Context mContext) {
        AlertUtils.showLoding(mContext);
        map = new ArrayMap<>();
        map.put("bookid", readingDetailsView.getBookId());
        map.put("token", Cache.getToken());
        UpdateFractory.getBuild()
                .name("BookdetailCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<BookDetailsBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(BookDetailsBean poiBean, String msg) {
                        readingDetailsView.onSuccess(poiBean);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        AlertUtils.disMissLoding();
                    }
                });
    }


    @Override
    public void collection(boolean status, final CallBackColonyContract.OnOperation onOperation) {
        map = new ArrayMap<>();
        map.put("bookid", readingDetailsView.getBookId());
        map.put("token", Cache.getToken());
        map.put("status", status ? "0" : "1");
        UpdateFractory.getBuild()
                .name("AddmyCollectionCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem poiBean, String msg) {
                        onOperation.OperationCallBack("");
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    @Override
    public void follow(String status, final CallBackColonyContract.OnOperation onOperation) {
        map = new ArrayMap<>();
        map.put("followid", readingDetailsView.getFollowId());
        map.put("token", Cache.getToken());
        map.put("status", status);

        UpdateFractory.getBuild()
                .name("AddmyFollowCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem poiBean, String msg) {
                        onOperation.OperationCallBack("");
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    @Override
    public void operationBook() {
        map = new ArrayMap<>();
        map.put("bookid", readingDetailsView.getBookId());
        map.put("token", Cache.getToken());
        map.put("status", readingDetailsView.getStatus());
        if (readingDetailsView.getStatus().equals("4")) {
            map.put("typeid", "");
            map.put("content", "不想看了");
        }
        if (!readingDetailsView.getStatus().equals("1")) {
            map.put("logid", readingDetailsView.getLogId());
        }
        UpdateFractory.getBuild()
                .name("OperationBookCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem item, String msg) {
                        readingDetailsView.onOperationSuccess(item);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }
}
